﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Newtonsoft.Json;
using System.Web.Script.Serialization;
using Newtonsoft.Json.Linq;
using DrawManage.Tools;


namespace DrawManage.Controllers
{
    public class HomeController : Controller
    {

        BaseDao dao = new BaseDao();
        public ActionResult Index()
        {
            if (Request.Cookies["id"] == null || Request.Cookies["id"].ToString() == "")
            {
                Response.Cookies["id"].Value = "";
                Response.Cookies["id"].Expires = DateTime.Now.AddDays(-1);
                return View("Login");
            }
            //获取用户名
            string sql = "select * from t_d_user where Id=" + Request.Cookies["id"].Value;
            var list = dao.GetList(sql, new string[] { "UserName", "role" });
            if(list.Count==1)
            {
                ViewBag.UserName = list[0]["UserName"].ToString();
                ViewBag.Role = list[0]["role"].ToString();
                return View("Index");
            }
            else
            {
                Response.Cookies["id"].Value = "";
                Response.Cookies["id"].Expires = DateTime.Now.AddDays(-1);

                return View("Login");
            }

        }


        public ActionResult Login()
        {
            ViewBag.Msg = "";
            return View("Login");
        }

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult LoginValidate()
        {
            string UserName = Request.Form["UserName"] == null ? null : Request.Form["UserName"].ToString();
            string PassWord = Request.Form["PassWord"] == null ? null : Request.Form["PassWord"].ToString();
            if (UserName == null)
            {
                ViewBag.Msg = "用户名错误";
                return View("Login");
            }
            string sql = "select count(Id) as num from t_d_User where UserName='" + UserName + "'";
            Dictionary<string, object> map = dao.GetList(sql, new string[] { "num" })[0];
            if (map["num"].ToString() != "1")
            {
                ViewBag.Msg = "用户名错误";
                return View("Login");
            }
            sql = "select * from t_d_User where UserName='" + UserName + "' and PassWord='" + PassWord + "'";
            var UserList = dao.GetList(sql, new string[] { "id", "role" });
            if(UserList.Count!=1)
            {
                
                Response.Cookies["id"].Value = "";
                Response.Cookies["id"].Expires = DateTime.Now.AddDays(-1);
                ViewBag.Msg = "用户名或密码错误";
                return View("Login");
            }
            var UserMap = UserList[0];

            Response.Cookies["id"].Value = UserMap["id"].ToString();
            Response.Cookies["id"].Expires = DateTime.Now.AddDays(15);
            Response.Cookies["role"].Value = UserMap["role"].ToString();
            Response.Cookies["role"].Expires = DateTime.Now.AddDays(15);

            return RedirectToAction("Index", "Home");

        }

        public ActionResult Logout()
        {
            Response.Cookies["id"].Value = "";
            Response.Cookies["id"].Expires = DateTime.Now.AddDays(-1);

            return View("Login");
        }



        public ActionResult MainTree()
        {
            string JsonStr = "";
            JsonStr = zTree.RebuildTree();
            //JsonStr = RebuildTree();

            //得到json字符串
            ViewBag.TreeJsonStr = JsonStr;

            return View("MainTree");
        }

        public ActionResult PassWord()
        {
            return View("PassWord");
        }


        public ActionResult GetDraw()
        {
            string JsonStr = "";
            JsonStr = zTree.RebuildTree();

            //得到json字符串
            ViewBag.TreeJsonStr = JsonStr;

            return View("GetDraw");

        }


        public ActionResult UserView()
        {
            return View("User");
        }


        public ActionResult DrawCombo()
        {
            return View("DrawCombo");
        }


        public ActionResult DrawInstall()
        {
            return View("DrawInstall");
        }


        public ActionResult TestShadow()
        {
            return View("TestShadow");
        }



    }
}